﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
namespace lcs.Web
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string keyword = context.Request.QueryString["keyword"];
            string sname = context.Request.QueryString["sname"];
            if (keyword != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串 
                string dogid = "";
                if(context.Request.Cookies["dogid"]!=null)
                    dogid=context.Request.Cookies["dogid"].Value.ToString();
                //string [] test = GetFilteredList(keyword, dogid, context);
                string jsonString = serializer.Serialize(GetFilteredList(keyword, dogid, context));
                context.Response.Write(jsonString); // 返回客户端json格式数据  
            }

            if (sname != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串    
                string jsonString = serializer.Serialize(GetFilteredListsname(sname));
                context.Response.Write(jsonString); // 返回客户端json格式数据  
            }
            if (context.Request["key"] != null && context.Request["key"] != "")
            {

                context.Response.Write(GetLikeProjectTitle(System.Web.HttpUtility.UrlDecode(context.Request["key"])));
                context.Response.Charset = "gb2312";
                //context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.ContentType = "application/json";
                context.Response.End();  
            }
                 
        }
        public String GetLikeProjectTitle(String keyword)
        {
            String strJson = String.Empty;
            Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
            DataSet ds = bll.GetListname(" projectName like '%" + keyword + "%'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                //strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return strJson;
        }
        private List<Maticsoft.Model.project> GetFilteredList(string keyword, string dogid, HttpContext context)
        {
            if (!DecodeDog(dogid,context))
                return null;
            Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
            DataSet ds = bll.GetListname(" projectname like '%" + keyword + "%'");

            List<Maticsoft.Model.project> proList = new List<Maticsoft.Model.project>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Maticsoft.Model.project p = new Maticsoft.Model.project();
                p.projectName = ds.Tables[0].Rows[i]["projectname"].ToString();
                p.proid = ds.Tables[0].Rows[i]["proid"].ToString();
                //p.projectId = ds.Tables[0].Rows[i]["proid"].ToString();
                //projectId
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                //proList.Add(ds.Tables[0].Rows[i]["projectname"].ToString());
                proList.Add(p);
            }

            return proList;
        }

        private string[] GetFilteredListsname(string sname)
        {
            lcs.BLL.supplierinfo bll = new lcs.BLL.supplierinfo();
            DataSet ds = bll.GetList(" sname like '%" + sname + "%'");

            List<string> proList = new List<string>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                proList.Add(ds.Tables[0].Rows[i]["sname"].ToString());
            }

            return proList.ToArray();
        }


        public bool DecodeDog(string dogid, HttpContext context)
        {
            if (isindog(context))
                return true;
            if (dogid == "")
                return false;
            else
            {
                if (dogid != getdogid(context))
                    return false;
                else
                    return true;
            }

        }
        public bool isindog(HttpContext context)
        {
            string name = getuname(context);
            string s = ConfigurationManager.AppSettings["dogusers"];
            if (s != null)
            {
                if (s == "*")
                    return true;
                string[] ss = s.Split(',');
                foreach (string m in ss)
                {
                    if (m == name)
                        return true;
                }
            }
            return false;
        }
        public string getdogid(HttpContext context)
        {
            string dep = "";
            if (context.Session["login"] != null)
            {
                DataSet ds = context.Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                dep = ds.Tables[0].Rows[0]["dogid"].ToString();
            }
            return dep;
        }
        public string getuname(HttpContext context)
        {
            string dep = "";
            if (context.Session["login"] != null)
            {
                DataSet ds = context.Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                dep = ds.Tables[0].Rows[0]["user_login"].ToString();
            }
            return dep;
        }
        //private Dictionary<string, string> GetFilteredList(string keyword)
        //{
        //    Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
        //    DataSet ds = bll.GetListname(" projectname like '%" + keyword + "%'");

        //    Dictionary<string, string> proList = new Dictionary<string, string>();

        //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    {
        //        //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
        //        proList.Add(ds.Tables[0].Rows[i]["projectname"].ToString(), ds.Tables[0].Rows[i]["proid"].ToString());
        //    }

        //    return proList;
        //}

        //private string[] GetFilteredList(string keyword)
        //{
        //    Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
        //    DataSet ds = bll.GetListname(" projectname like '%" + keyword + "%'");

        //    List<string> proList = new List<string>();

        //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    {
        //        //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
        //        proList.Add("label:" + ds.Tables[0].Rows[i]["projectname"].ToString() + ",value:" + ds.Tables[0].Rows[i]["proid"].ToString());
        //    }

        //    return proList.ToArray();
        //}

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}